package com.yzdbx.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.yzdbx.utils.DbUtil;
import com.yzdbx.utils.GenUtil;

import java.math.BigDecimal;
import java.sql.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;

import static com.yzdbx.utils.DbUtil.getMaxId;
import static com.yzdbx.utils.GenUtil.*;

/**
 * @BelongsProject: SERVEVICE-DATA-GEN
 * @BelongsPackage: com.yzdbx.task
 * @Author: lk
 * @CreateTime: 2025-05-14
 * @Description: TODO
 * @Version: 1.0
 */
@SuppressWarnings("all")
public class TroubleShootingTaskRunnable implements Exe {


    private Connection connection;

    private static final Map<String, List<String>> textileFaultMap = new HashMap<>() {{
        put("机械类故障", Arrays.asList("主轴轴承异常磨损导致振动超标", "齿轮箱啮合不良产生周期性异响", "同步带老化开裂造成传动打滑", "罗拉弯曲变形影响纤维牵伸均匀性", "针布钝化导致梳理效果下降", "锭子动态不平衡引发纱线张力波动", "连杆机构间隙过大产生冲击噪音", "导轨润滑不足造成运动卡滞", "弹簧弹性失效影响加压稳定性", "密封圈破损引起润滑脂泄漏"));

        put("电气类故障", Arrays.asList("电机绕组短路导致过热保护跳闸", "变频器IGBT模块击穿造成输出异常", "接触器触点烧蚀引发控制回路失效", "编码器信号干扰导致定位偏差", "伺服驱动器过载报警停机", "PLC输入模块通道损坏引发现场信号丢失", "电磁阀线圈断路致使气动执行失效", "限位开关氧化导致位置检测失灵", "电缆绝缘层破损引发电气短路", "触摸屏死机造成人机交互中断"));

        put("工艺类故障", Arrays.asList("染液pH值失控导致色差超标", "烘箱温度梯度不均造成布面干燥不匀", "浆料粘度异常引发经纱上浆不足", "纬纱张力失调产生布面纬斜", "印花网版堵塞造成图案残缺", "定型机超喂不稳定导致门幅波动", "并条机牵伸倍数偏差影响条干CV值", "络筒机卷绕密度不均形成葫芦纱", "预缩机湿度控制失效引发布料缩率异常", "烧毛机火焰强度不稳导致织物烧毛不净"));

        put("传感器类故障", Arrays.asList("温度传感器漂移显示值偏离实际", "压力变送器膜片损坏输出固定值", "光电探纬器灵敏度下降漏检断纬", "霍尔传感器磁铁脱落丧失转速检测", "接近开关感应距离缩短误触发", "张力传感器零点漂移反馈失真", "色差检测仪镜头污染误判色牢度", "湿度传感器结露导致RH值跳变", "振动加速度计电缆断裂信号丢失", "红外测温仪发射率设置错误显示虚高"));
    }};


    public TroubleShootingTaskRunnable(Connection connection) {
        this.connection = connection;
    }

    public boolean execute(int count) throws SQLException {
        LocalDate localDate = CUR_DATE.get();
        if (RandomUtil.randomBoolean()) return Boolean.TRUE;
        count = 1;
        int maxId = getMaxId(connection, "schedulingOfSbbx01MalfunctionRep");

        PreparedStatement statement = connection.prepareStatement("select * from equipmentCounterStatements");
        List<LinkedHashMap<String, Object>> shebeis = DbUtil.resultSetToList(statement.executeQuery());

        statement = connection.prepareStatement("select * from corp_member");
        List<String> members = DbUtil.resultSetToList(statement.executeQuery()).stream().map(map -> map.get("memberId").toString()).toList();


        for (int i = 0; i < count; i++) {
            statement = connection.prepareStatement("insert into schedulingOfSbbx01MalfunctionRep (id,f6f1,deviceNumber,deviceInformation,faultType,synchronisingFolder,levelOfUrgency,theWarranty,renovationTime,maintainer,maintenanceStatus,startOfMaintenance,completionOfMaintenance,maintenanceTime,analysisOfTheCauseOfTheFailure,maintenanceCosts) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            statement.setInt(1, ++maxId);
            statement.setString(2, getNoFromTable(connection,"BX-", "schedulingOfSbbx01MalfunctionRep","f6f1"));
            LinkedHashMap<String, Object> shebei = RandomUtil.randomEle(shebeis);
            statement.setString(3, shebei.get("stationNumber").toString());
            statement.setString(4, shebei.get("f65e").toString());
            String key = RandomUtil.randomEle(new ArrayList<>(textileFaultMap.keySet()));
            statement.setString(5, key);
            statement.setString(6, RandomUtil.randomEle(textileFaultMap.get(key)));
            statement.setString(7, RandomUtil.randomEle(new String[]{"普通", "紧急", "非常紧急"}));
            String member = RandomUtil.randomEle(members);
            statement.setString(8, member);
            long epochMilli = localDate.atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli();
            Timestamp bxsj = new Timestamp(epochMilli + (localDate.equals(LocalDate.now()) ? RandomUtil.randomLong(System.currentTimeMillis() - epochMilli) : RandomUtil.randomLong(24 * 60 * 60 * 1000)));
            statement.setTimestamp(9, bxsj);
            String wxr = RandomUtil.randomEle(members);
            statement.setString(10, wxr);
            statement.setString(11, "已维修");
            long l1 = 1000L;
            try {
               l1 = RandomUtil.randomLong(System.currentTimeMillis() - bxsj.getTime());
            }catch (Exception ex){}
            Timestamp wxsj = new Timestamp(bxsj.getTime() + l1);
            statement.setTimestamp(12, wxsj);
            long l = 1000L;
            try {
                l = RandomUtil.randomLong(System.currentTimeMillis() - wxsj.getTime());
            }catch (Exception ex){}
            Timestamp wxend = new Timestamp(wxsj.getTime() + l);
            statement.setTimestamp(13, wxend);
            statement.setInt(14, (int) (wxend.getTime() - wxsj.getTime()) / 1000 / 60);
            statement.setString(15, key);
            statement.setBigDecimal(16, RandomUtil.randomBigDecimal(new BigDecimal(200), new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_UP));

            statement.execute();

        }

        return Boolean.TRUE;
    }


    public static void main(String[] args) {
        System.out.println(RandomUtil.randomLong(0));
    }


}
